# -*- encoding=utf8 -*-

__author__ = "code哥"
import os
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
from conn_pool import get_redis, REDIS_LIST_KEY

rs = get_redis()
rs.flushall()


class FileMonitorHandler(FileSystemEventHandler):
    def __init__(self):
        FileSystemEventHandler.__init__(self)

    def on_created(self, event):
        if not event.is_directory:
            print(event.src_path)
            rs.lpush(REDIS_LIST_KEY, event.src_path)

    # 判断单个脚本截图数量大于100张，开启删除老图


def watch_handler():
    handler = FileMonitorHandler()
    observer = Observer()
    # recursive递归
    path = '{path}/static/'.format(path=os.path.dirname(os.path.abspath(__file__)))
    observer.schedule(handler,
                      path=path,
                      recursive=True)
    print('===================:' + path)
    observer.start()
    observer.join()


if __name__ == '__main__':
    watch_handler()
